//
//  AddImage.m
//  Line
//
//  Created by HCW on 15/1/11.
//  Copyright (c) 2015年 HCW. All rights reserved.
//

static void *AddImageBlockAssociationKey = &AddImageBlockAssociationKey;

#import "HCWAddImage.h"
#import "AppDelegate.h"
#import <objc/runtime.h>

@implementation HCWAddImage

- (void)addImageFinishBlock: (AddImageFinishBlock)finishBlock
{
    objc_setAssociatedObject(self, AddImageBlockAssociationKey, finishBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    UIActionSheet *sheet;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
    }
    else {
        sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", nil];
    }
    
    UIViewController *ctrl = [(AppDelegate *)[UIApplication sharedApplication].delegate window].rootViewController;
    [sheet showInView:ctrl.view];
}

#pragma mark - UIActionSheet delegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    NSUInteger sourceType = 0;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
            case 0:
                // 相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                // 相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                // 取消
                return;
        }
    }
    else {
        if (buttonIndex == 0) {
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        } else {
            return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = sourceType;
    UIViewController *ctrl = [(AppDelegate *)[UIApplication sharedApplication].delegate window].rootViewController;
    [ctrl presentViewController:imagePickerController animated:YES completion:^{}];
    
}

#pragma mark - UIImagePickerController delegte

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        AddImageFinishBlock finishBlock = objc_getAssociatedObject(self, AddImageBlockAssociationKey);
        finishBlock(image);
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    UIViewController *ctrl = [(AppDelegate *)[UIApplication sharedApplication].delegate window].rootViewController;
    [ctrl dismissViewControllerAnimated:YES completion:^{}];
}

@end
